package cn.iocoder.yudao.module.design.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.pic.vo.*;
import cn.iocoder.yudao.module.design.convert.pic.PicConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.pic.PicDO;
import cn.iocoder.yudao.module.design.service.pic.PicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 图片")
@RestController
@RequestMapping("/design/pic")
@Validated
public class AppPicController {

    @Resource
    private PicService picService;

    @PostMapping("/create")
    @Operation(summary ="创建图片")
    @PreAuthorize("@ss.hasPermission('design:pic:create')")
    public CommonResult<Long> createPic(@Valid @RequestBody PicCreateReqVO createReqVO) {
        return success(picService.createPic(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新图片")
    @PreAuthorize("@ss.hasPermission('design:pic:update')")
    public CommonResult<Boolean> updatePic(@Valid @RequestBody PicUpdateReqVO updateReqVO) {
        picService.updatePic(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除图片")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:pic:delete')")
    public CommonResult<Boolean> deletePic(@RequestParam("id") Long id) {
        picService.deletePic(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得图片")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:pic:query')")
    public CommonResult<PicRespVO> getPic(@RequestParam("id") Long id) {
        PicDO pic = picService.getPic(id);
        return success(PicConvert.INSTANCE.convert(pic));
    }

    @GetMapping("/list")
    @Operation(summary ="获得图片列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048" )
    @PreAuthorize("@ss.hasPermission('design:pic:query')")
    public CommonResult<List<PicRespVO>> getPicList(@RequestParam("ids") Collection<Long> ids) {
        List<PicDO> list = picService.getPicList(ids);
        return success(PicConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得图片分页")
    @PreAuthorize("@ss.hasPermission('design:pic:query')")
    public CommonResult<PageResult<PicRespVO>> getPicPage(@Valid PicPageReqVO pageVO) {
        PageResult<PicDO> pageResult = picService.getPicPage(pageVO);
        return success(PicConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出图片 Excel")
    @PreAuthorize("@ss.hasPermission('design:pic:export')")
   
    public void exportPicExcel(@Valid PicExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PicDO> list = picService.getPicList(exportReqVO);
        // 导出 Excel
        List<PicExcelVO> datas = PicConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "图片.xls", "数据", PicExcelVO.class, datas);
    }

}
